package ferp.android.dialogs;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.managers.ProfileManager;
import ferp.android.widgets.BidView;
import ferp.core.game.Bid;
import ferp.core.game.Game;
import ferp.core.game.Settings;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 3/27/13 12:59 PM
 */
public class ConfirmationDialog extends YesNoDialog
{
  public static final BidConfirmationChanger bcc = new BidConfirmationChanger();
  public static final ContractConfirmationChanger ccc = new ContractConfirmationChanger();

  public static ConfirmationDialog create(Context context, Listener listener, int requestCode)
  {
    Profile profile = ProfileManager.instance(context).getCurrentProfile();

    if (profile == null)
      return null;

    Game game = profile.game();

    return game == null || game.input.bid.type == null ? null : new ConfirmationDialog(context, listener, requestCode);
  }

  public void set(int title, CharSequence text, Settings.Confirmations confirmations, Settings.Confirmations.Changer scc, Settings.Confirmation selected)
  {
    set(title, confirmations, scc, selected);

    this.text.setText(text);

    this.bid.layout.setVisibility(View.GONE);
    this.text.setVisibility(View.VISIBLE);
  }

  public void set(int title, String text1, Bid bid, String text2, Settings.Confirmations confirmations, Settings.Confirmations.Changer scc, Settings.Confirmation selected)
  {
    set(title, confirmations, scc, selected);

    this.bid.text1.setText(text1);
    this.bid.view.show(bid);
    this.bid.text2.setText(text2);

    this.text.setVisibility(View.GONE);
    this.bid.layout.setVisibility(View.VISIBLE);
  }

  private static final class BidConfirmationChanger implements Settings.Confirmations.Changer
  {
    @Override
    public void change(Settings.Confirmations confirmations, Settings.Confirmation value) {confirmations.bid = value;}
  }

  private static final class ContractConfirmationChanger implements Settings.Confirmations.Changer
  {
    @Override
    public void change(Settings.Confirmations confirmations, Settings.Confirmation value) {confirmations.contract = value;}
  }

  private static final class Bid_
  {
    private LinearLayout layout;
    private BidView view;
    private TextView text1;
    private TextView text2;
  }

  private final TextView text;
  private final Bid_ bid = new Bid_();
  private final SeekBar slider;
  private final TextView[] options = new TextView[Settings.Confirmation.values().length];
  private Settings.Confirmations confirmations;
  private Settings.Confirmations.Changer changer;

  private ConfirmationDialog(Context context, Listener listener, int requestCode)
  {
    super(context, requestCode, R.layout.dialog_confirmation, listener);

    // resolve message text views
    text = resolveTextView(R.id.dlg_confirmation_text);
    resolveTextView(R.id.dlg_confirmation_note);

    bid.layout = (LinearLayout)findViewById(R.id.dlg_confirmation_bid_layout);
    bid.view = (BidView)findViewById(R.id.dlg_confirmation_bid);
    bid.text1 = resolveTextView(R.id.dlg_confirmation_text1);
    bid.text2 = resolveTextView(R.id.dlg_confirmation_text2);

    options[0] = resolveTextView(R.id.dlg_confirmation_text_always);
    options[1] = resolveTextView(R.id.dlg_confirmation_text_intelligent);
    options[2] = resolveTextView(R.id.dlg_confirmation_text_never);

    for (int i = 0; i < options.length; ++i)
    {
      final int id = i;

      options[i].setOnTouchListener(new View.OnTouchListener()
      {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
          slider.setProgress(id);
          setSelectedOption(id);
          changer.change(confirmations, Settings.Confirmation.values()[id]);

          return true;
        }
      });
    }

    // resolve slider
    slider = (SeekBar)findViewById(R.id.dlg_confirmation_slider);
    slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    {
      @Override
      public void onProgressChanged(SeekBar seekBar, int i, boolean b)
      {
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar)
      {
      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar)
      {
        setSelectedOption(seekBar.getProgress());
        changer.change(confirmations, Settings.Confirmation.values()[seekBar.getProgress()]);
      }
    });
  }

  private TextView resolveTextView(int id)
  {
    TextView tv = (TextView)findViewById(id);

    tv.setTypeface(GUI.Font.BOLD);
    return tv;
  }

  private void setSelectedOption(int progress)
  {
    for (int i = 0; i < options.length; ++i)
      options[i].setTextColor(progress == i ? GUI.Color.TITLE : Color.WHITE);
  }

  private void set(int title, Settings.Confirmations confirmations, Settings.Confirmations.Changer scc, Settings.Confirmation selected)
  {
    this.title.setText(title);

    slider.setProgress(selected.ordinal());
    setSelectedOption(selected.ordinal());

    this.confirmations = confirmations;
    this.changer = scc;
  }
}
